<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
abstract class banco{
    // Parameter Names
    public $server      = "localhost";
    public $dbname      = "bioall";
    public $username    = "root";
    public $pwd         = "bioall0109";
    public $conection   = null;
    public $dataset     = null;
    public $rowsafected = -1;


    // Class
    public function __construct() {
        $this->connect();
    } // End construct
    
    public function __destruct() {
        if($this->connect != NULL):
            mysql_close($this->conection);
        endif;
    } // End desctruct

    public function connect(){
        $this->conection = mysql_connect($this->server, $this->username,$this->pwd,true) 
                or die($this->errorhandling(__FILE__,__FUNCTION__,  mysql_errno(), mysql_error(), true));
        mysql_select_db($this->dbname) 
                or die ($this->errorhandling(__FILE__,__FUNCTION__,  mysql_errno(), mysql_error(), true));
        mysql_query("SET NAMES 'utf8'");
        mysql_query("SET character_set_connection=utf8");
        mysql_query("SET character_set_client=utf8");
        mysql_query("SET character_set_results=utf8");        
    } // End conection
    
     public function insert($object){
         $sql = "INSERT INTO ".$object->table." (";
         for($i=0;$i<count($object->fields_values);$i++):
             $sql .= key($object->fields_values);
             if($i < count($object->fields_values)-1):
                 $sql .= ", ";
             else:
                 $sql .= ") ";
             endif;
             next($object->fields_values);
         endfor;
         reset($object->fields_values);
         
         $sql .= "VALUES (";
         for($i=0;$i<count($object->fields_values);$i++):
             $sql .= is_numeric($object->fields_values[key($object->fields_values)])? 
                    $object->fields_values[key($object->fields_values)] :
                    "'".$object->fields_values[key($object->fields_values)]."'";
         
         
             if($i < count($object->fields_values)-1):
                 $sql .= ", ";
             else:
                 $sql .= ") ";
             endif;
             next($object->fields_values);
         endfor;         

         return $this->execSQL($sql);
         
     } // insert

     public function execSQL($sql=NULL){
         if($sql!=NULL):
            $query = mysql_query($sql) or $this->errorhandling(__FILE__,__FUNCTION__);
            $this->rowsafected = mysql_affected_rows($this->conection);
            if($sql!=null):
                $this->dataset=$query;
                return $query;
            else:
                return $this->rowsafected;
            endif;
         else:
            $this->errorhandling(__FILE__,__FUNCTION__,null,'Sql not informed in routine.',FALSE);   
         endif;
     } // execSQL

     public function selectAll($object){
         $sql = "SELECT * FROM ".$object->table;
         if($object->extra_selects!=null):
             $sql .= " ".$object->extra_selects;
         endif;
         return $this->execSQL($sql);
     } //selectAll

     public function selectFields($object){
         $sql = "SELECT ";
         for($i=0;$i<count($object->fields_values);$i++):
             $sql .= key($object->fields_values);
             if($i < count($object->fields_values)-1):
                 $sql .= ", ";
             else:
                 $sql .= " ";
             endif;
             next($object->fields_values);
         endfor;
         
         $sql .= " FROM ".$object->table;
         if($object->extra_selects!=null):
             $sql .= " ".$object->extra_selects;
         endif;
         return $this->execSQL($sql);
     } //selectFields
     
     public function selectdata($type=null){
         switch (strtolower($type)):
             case "array":
                 return mysql_fetch_array($this->dataset);
                 break;
             case "assoc":
                 return mysql_fetch_assoc($this->dataset);
                 break; 
             case "object":
                 return mysql_fetch_object($this->dataset);
                 break;
             default:
                 return mysql_fetch_object($this->dataset);
             endswitch;
     } // selectdata

     public function update($object){
         $sql = "UPDATE ".$object->table." SET ";
         for($i=0;$i<count($object->fields_values);$i++):
             $sql .= key($object->fields_values)."=";
             $sql .= is_numeric($object->fields_values[key($object->fields_values)])? 
                    $object->fields_values[key($object->fields_values)] :
                    "'".$object->fields_values[key($object->fields_values)]."'";
             
             if($i < count($object->fields_values)-1):
                 $sql .= ", ";
             else:
                 $sql .= " ";
             endif;
             next($object->fields_values);
         endfor;
         $sql .= " WHERE ".$object->fieldspk."=";
         $sql .= is_numeric($object->valuepk) ? $object->valuepk: "'".$object->valuepk."'";
         
         return $this->execSQL($sql);
         
     } // update
     
     public function delete($object){
         $sql = "DELETE FROM ".$object->table;
         $sql .= " WHERE ".$object->fieldspk."=";
         $sql .= is_numeric($object->valuepk) ? $object->valuepk: "'".$object->valuepk."'";
         
         echo ($sql);
         
         return $this->execSQL($sql); 
     } //delete

     public function errorhandling($file=NULL,$routine=NULL,$errorNum=NULL, $errorMsg=NULL,$exeption=FALSE ){
         
         if($file==NULL) 
             $file="Not informated !!!";
         if($routine==NULL) 
             $routine="Not Informated !!!";
         if($errorNum==NULL) 
             $errorNum=mysql_errno($this->conection);
         if($errorMsg==NULL) 
             $errorMsg=mysql_erro($this->conection);
         
         $result = 'Erros found:<br /> <br />
             <strong>File:</strong>     '.$file.'<br/>
             <strong>Routine:</strong>  '.$routine.'<br/>
             <strong>Id:</strong>       '.$errorNum.'<br/>
             <strong>Message:</strong>  '.$errorMsg;
         
         if($exeption==FALSE):
             echo($result);
         else:
             die($result);
         endif;
     }
     
     
} // abstract banco

?>
